package com.situ.thymeleaf.controller;

import com.situ.thymeleaf.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class IndexController {

    @RequestMapping("/index")
    public String index(Model model) {
        System.out.println("IndexController.index");
        model.addAttribute("message", "HelloWorld");

        Student student = new Student(1, "zhangsan", 23, new Date());
        model.addAttribute("student", student);

        List<Student> list = new ArrayList<>();
        Student student1 = new Student(1, "zhangsan1", 23, new Date());
        Student student2 = new Student(2, "zhangsan2", 23, new Date());
        Student student3 = new Student(3, "zhangsan3", 23, new Date());
        list.add(student1);
        list.add(student2);
        list.add(student3);
        model.addAttribute("list", list);

        return "index";
    }

    @RequestMapping("/data")
    public String data(HttpServletRequest request, HttpSession session) {
        System.out.println("IndexController.data");
        Student student1 = new Student(1, "zhansgan1", 23, new Date());
        request.setAttribute("student1", student1);

        Student student2 = new Student(2, "zhansgan2", 23, new Date());
        session.setAttribute("student2", student2);



        Student student3 = new Student(3, "zhangsan3", 23, new Date());
        ServletContext servletContext = request.getServletContext();
        servletContext.setAttribute("student3", student3);
        return "data";
    }


    @RequestMapping("/set")
    public String set(Model model) {
        System.out.println("IndexController.set");
        Set<String> allNames = new HashSet<>();
        List<Integer> allIds = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            allNames.add("boot-" + i);
            allIds.add(i);
        }
        model.addAttribute("names", allNames);
        model.addAttribute("ids", allIds);

        model.addAttribute("mydate", new Date());
        return "set";
    }


}
